<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 4 编写函数 获取四位随机验证码 "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" -->
    <script>
        var arr = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        var yzm = "";
        for (var i = 1; i <= 4; i++) {
            // 因为只有4为验证码 所以只用循环四次
            var a = Math.floor(Math.random() * arr.length)
            yzm = yzm + arr[a];
        }
        console.log(yzm);


        // 4 编写函数 获取四位随机验证码 "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" -->
        function getCode(n) {
            var codeStr =
                "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
            var len = codeStr.length;
            var code = ""; // 验证码
            for (var i = 1; i <= n; i++) {
                var idx = Math.floor(Math.random() * len);
                code += codeStr[idx];
            }
            return code;
        }

        console.log(getCode(4));
    </script>
</body>

</html>